package com.xuexiang.xuidemo.slice.imageview.edit;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.Slider;
import ohos.bundle.IBundleManager;
import ohos.media.image.PixelMap;
import ohos.utils.net.Uri;

import com.xuexiang.xui_lib.component.imageview.crop.CropDrawableUtils;
import com.xuexiang.xui_lib.component.imageview.edit.PhotoEnhance;
import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.slice.BaseAbilitySlice;
import com.xuexiang.xuidemo.util.XToastUtils;

public class ImageEnhanceSlice extends BaseAbilitySlice implements Component.ClickedListener, Slider.ValueChangedListener {
    private Slider sBSaturation;
    private Slider sBBrightness;
    private Slider sBContrast;
    private Image ivContent;
    private PhotoEnhance mPhotoEnhance;
    private static final int MY_PERMISSIONS_REQUEST_STORE = 100;
    private static final String[] storePermissions =
        {
            "ohos.permission.READ_USER_STORAGE",
            "ohos.permission.WRITE_USER_STORAGE"

        };
    /**
     * 选择系统图片Request Code
     */
    public static final int REQUEST_IMAGE = 112;
    private int mProgress = 0;

    @Override
    protected void initComponents() {
        initView();
    }

    @Override
    protected int getUIContentLayoutId() {
        return ResourceTable.Layout_image_enhance_layout;
    }

    private void initView() {
        sBSaturation = (Slider) findComponentById(ResourceTable.Id_sb_saturation);
        sBBrightness = (Slider) findComponentById(ResourceTable.Id_sb_brightness);
        sBContrast = (Slider) findComponentById(ResourceTable.Id_sb_contrast);
        ivContent = (Image) findComponentById(ResourceTable.Id_iv_content);
        findComponentById(ResourceTable.Id_image_back).setClickedListener(this);
        findComponentById(ResourceTable.Id_text_select).setClickedListener(this);
        sBSaturation.setValueChangedListener(this);
        sBBrightness.setValueChangedListener(this);
        sBContrast.setValueChangedListener(this);
        mPhotoEnhance = new PhotoEnhance();
        sBSaturation.setMaxValue(255);
        sBSaturation.setProgressValue(128);
        sBBrightness.setMaxValue(255);
        sBBrightness.setProgressValue(128);
        sBContrast.setMaxValue(255);
        sBContrast.setProgressValue(128);
        sBSaturation.setLayoutRefreshedListener(new Component.LayoutRefreshedListener() {
            @Override
            public void onRefreshed(Component component) {
                sBSaturation.setMaxValue(255);
                sBSaturation.setProgressValue(sBSaturation.getProgress());
                sBBrightness.setMaxValue(255);
                sBBrightness.setProgressValue(sBBrightness.getProgress());
                sBContrast.setMaxValue(255);
                sBContrast.setProgressValue(sBContrast.getProgress());
            }
        });
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_image_back:
                terminate();
                break;
            case ResourceTable.Id_text_select:
                selectPic();
                break;
        }
    }

    private void selectPic() {
        if (verifySelfPermission("ohos.permission.READ_USER_STORAGE") != IBundleManager.PERMISSION_GRANTED
            || verifySelfPermission("ohos.permission.WRITE_USER_STORAGE") != IBundleManager.PERMISSION_GRANTED) {
            // 应用未被授予权限
            if (canRequestPermission("ohos.permission.READ_USER_STORAGE") || canRequestPermission("ohos.permission.WRITE_USER_STORAGE")) {
                // 是否可以申请弹框授权(首次申请或者用户未选择禁止且不再提示)
                requestPermissionsFromUser(
                    storePermissions, MY_PERMISSIONS_REQUEST_STORE);
            }
        } else {
            Intent intent = new Intent();
            intent.setAction("android.intent.action.GET_CONTENT");
            intent.addFlags(Intent.FLAG_NOT_OHOS_COMPONENT);
            intent.setType("image/*");
            startAbilityForResult(intent, REQUEST_IMAGE);
        }
    }

    @Override
    protected void onAbilityResult(int requestCode, int resultCode, Intent data) {
        super.onAbilityResult(requestCode, resultCode, data);
        //选择系统图片并解析
        if (requestCode == REQUEST_IMAGE) {
            if (data != null) {
                Uri uri = data.getUri();
                if (uri != null) {
                    PixelMap pixelMap = CropDrawableUtils.displayPixelMap(this, uri);
                    mPhotoEnhance.setBitmap(pixelMap);
                    ivContent.setPixelMap(pixelMap);
                    resetProgress();
                }
            }
        }
    }

    @Override
    public void onProgressUpdated(Slider slider, int i, boolean b) {
        mProgress = i;
    }

    private void resetProgress() {
        sBSaturation.setProgressValue(128);
        sBBrightness.setProgressValue(128);
        sBContrast.setProgressValue(128);
        mProgress = 128;
    }

    @Override
    public void onTouchStart(Slider slider) {
    }

    @Override
    public void onTouchEnd(Slider slider) {
        if (!mPhotoEnhance.hasSetBitmap()) {
            XToastUtils.warning("请先选择图片");
            return;
        }
        int type = 0;
        switch (slider.getId()) {
            case ResourceTable.Id_sb_saturation:
                mPhotoEnhance.setSaturation(mProgress);
                type = PhotoEnhance.ENHANCE_SATURATION;
                break;
            case ResourceTable.Id_sb_brightness:
                mPhotoEnhance.setBrightness(mProgress);
                type = PhotoEnhance.ENHANCE_BRIGHTNESS;
                break;
            case ResourceTable.Id_sb_contrast:
                mPhotoEnhance.setContrast(mProgress);
                type = PhotoEnhance.ENHANCE_CONTRAST;
                break;
        }
        ivContent.setPixelMap(mPhotoEnhance.handleImage(type));
    }
}
